package edu.gxmzu.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import edu.gxmzu.task.entity.Message;
import edu.gxmzu.task.mapper.MessageMapper;
import edu.gxmzu.task.service.MessageService;
import edu.gxmzu.task.utils.Result;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    private MessageMapper messageMapper;
    @Override
    public Result deleteMessage(String userId, List<String> ids) {
        if(ids.isEmpty()) return Result.badRequest("数组不能为空");
        LambdaQueryWrapper<Message> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Message::getUserId,userId);
        lambdaQueryWrapper.in(Message::getId,ids);
        int row= messageMapper.delete(lambdaQueryWrapper);
        if (row!=ids.size()) return Result.badRequest("删除消息失败");
        else  return Result.ok("删除消息成功");
    }

    @Override
    public Result readMessage(String userId, List<String> ids) {
        if(ids.isEmpty()) return Result.badRequest("数组不能为空");
        LambdaUpdateWrapper<Message> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Message::getUserId,userId);
        lambdaUpdateWrapper.in(Message::getId,ids);
        lambdaUpdateWrapper.set(Message::getStatusId,1);
        int row=messageMapper.update(null,lambdaUpdateWrapper);
        if (row!=ids.size()) return Result.badRequest("已读消息失败");
        else  return Result.ok("已读消息成功");
    }
}
